﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TcomInvoice
{
    public partial class SearchCustomer : Form
    {
        public Customer SelectCustomer = null;
        DataHelper db = new DataHelper();

        public SearchCustomer()
        {
            InitializeComponent();
        }

        private void SearchCustomer_Load(object sender, EventArgs e)
        {
            foreach (Customer customer in db.GetListCustomer())
            {
                ListViewItem item = listView1.Items.Add(new ListViewItem { Text = customer.Organization, Tag = customer.CustomerID });
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = customer.PresentativeName });
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = customer.TaxCode });
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = customer.TelNo });
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = customer.Address });
            }
        }

        private void Filter(string Name, string Org, string Tel, string Tax)
        {
            listView1.Items.Clear();
            var query = db.GetListCustomer().Where(p => (p.PresentativeName ?? "").IndexOf(Name, StringComparison.CurrentCultureIgnoreCase) != -1
                && (p.Organization ?? "").IndexOf(Org, StringComparison.CurrentCultureIgnoreCase) != -1 
                && (p.TelNo ?? "").StartsWith(Tel)
                && (p.TaxCode ?? "").StartsWith(Tax));
            foreach (Customer customer in query)
            {
                ListViewItem item = listView1.Items.Add(new ListViewItem { Text = customer.Organization, Tag = customer.CustomerID });
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = customer.PresentativeName });
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = customer.TaxCode });
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = customer.TelNo });
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = customer.Address });
            }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            Filter(txtName.Text, txtOrg.Text, txtTelNo.Text, txtTaxCode.Text);
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            GetSelectedCustomer();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            GetSelectedCustomer();            
        }

        private void GetSelectedCustomer()
        {
            ListViewItem item = listView1.SelectedItems[0];
            if (item != null)
            {
                SelectCustomer = db.GetListCustomer().First(p => p.CustomerID == int.Parse(item.Tag.ToString()));
            }
            if (SelectCustomer != null)
            {
                this.Dispose();
            }
            else
            {
                MessageBox.Show("Chọn khách hàng lập hóa đơn.");
            }
        }
    }
}
